package com.zjitc.controller.admin;

import com.zjitc.model.User;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.InternalResourceView;

import javax.servlet.http.HttpSession;

/**
 * Create by IntelliJ IDEA
 *
 * @author: lhl
 * @date-Time: 2018/4/23 0023
 * @description:
 */
@Controller
@RequestMapping(value = "/admin")
public class AdminControl {

    /**
     * 规定，二次验证 管理员用户名和姓名一样，否则登录不成功
     */
    @RequestMapping(value = "/login")
    public ModelAndView validate(HttpSession session) {
        User user = (User) session.getAttribute("user");
        ModelAndView mv = new ModelAndView();
        if (user != null) {
            if (user.getUsername().equals(user.getName())) {
                mv.setView(new InternalResourceView("/admin/home.jsp"));
            } else {
                mv.setView(new InternalResourceView("/index.do"));
            }
        }
        return mv;
    }

    @RequestMapping(value = "/adminGoBack")
    public String adminBack(HttpSession session) {
        session.removeAttribute("user");
        return "redirect:/index.do";
    }
}
